/**
 * 
 */
package com.honghao.student.util;

import java.util.Locale;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

/**
 * @author honghao.li
 *
 */
public class MultiViewResover implements ViewResolver{
	
	@Setter @Getter
	private Map<String, ViewResolver> resolvers;  

	@Override
	public View resolveViewName(String viewName, Locale local) throws Exception {
		//获取后缀名称 
		int n = viewName.lastIndexOf(".");
		String suffix = "";
		// 没有默认使用“jsp“方式 解析,有的话截取.后面的字符串 这里一般是jsp,ftl,vm与配置文件中的<entry  
        // key="ftl">的key匹配
		if(n==(-1)){
			suffix = "jsp"; 
		}else{
			suffix = viewName.substring(n + 1);
	        // 取.前面的部分 那时真正的资源名.比如我们要使用hello.jsp 那viewName就应该是hello
	        viewName = viewName.substring(0, n);
		}
		// 根据.后面的字符串去获取托管的视图解析类对象  
        ViewResolver resolver = resolvers.get(suffix);
		if (resolver != null)  
            return resolver.resolveViewName(viewName, local); 
		return null;
	}
}
